$(document).ready(function() {
    
    var fromWho = "от кого";
    var yourFeed = "текст отзыва";
    var error1 = "<span>Заполните все поля</span>";
    var error2 = "Спасибо за ваш отзыв!";
    
    var loadingImg = $("#load_img");
    var img_cont = $("#img_cont");
    
    loadingImg.hide();
    img_cont.hide();
    
    var resultServerDiv = $("div#resultserver");
    var inputFrom = $("input#from_mail");
    var textFeed = $("textarea#feed");
    var feedbackForm = $("form#feedback_form");
    
    inputFrom.val(fromWho);
    inputFrom.css('color', color_letters);
    
    textFeed.val(yourFeed);
    textFeed.css('color', color_letters);
    
    /*GET FOCUS ON INPUT FIELD*/
    inputFrom.focus(function(){  
        if($.trim(inputFrom.val()) == fromWho){
            inputFrom.val("");
            inputFrom.css('color', color_letters_active);
        }
    });
    
    /*LOOSE FOCUS ON INPUT FIELD*/
    inputFrom.blur(function(){
        if( $.trim(inputFrom.val()) == ""){
            inputFrom.val(fromWho);
            inputFrom.css('color', color_letters);
        }
    });
    
    /*GET FOCUS ON INPUT FIELD*/
    textFeed.focus(function(){  
        if($.trim(textFeed.val()) == yourFeed){
            textFeed.val("");
            textFeed.css('color', color_letters_active);
        }
    });
    
    /*LOOSE FOCUS ON INPUT FIELD*/
    textFeed.blur(function(){
        if( $.trim(textFeed.val()) == ""){
            textFeed.val(yourFeed);
            textFeed.css('color', color_letters);
        }
    });
    
    /*CLEAR MESSAGE IF USER STARTS TO TYPE*/
    textFeed.keypress(function() {
        resultServerDiv.html("");
    });
    
    /*CLEAR MESSAGE IF USER STARTS TO TYPE*/
    inputFrom.keypress(function() {
        resultServerDiv.html("");
    });
    
    /*'SUBMIT' EVENT TO FEEDBACK FORM*/
    feedbackForm.submit(function() {
        
        var feedback = textFeed.val();
        var mailfrom = inputFrom.val();
        
        if (($.trim(textFeed.val()) === yourFeed) || ($.trim(inputFrom.val()) === fromWho) || ($.trim(textFeed.val()) === "") || ($.trim(inputFrom.val()) === "")) {
            resultServerDiv.hide();
            resultServerDiv.fadeIn("slow").html(error1);
        } 
        else {
            img_cont.show();
            loadingImg.show();
            
            $.post(path_ajax + "/user/sendFeed.php", {
                feed: feedback,
                mail: mailfrom
            },
            function(result) {
                
                var res = error2;
                
                if(result != true){
                    res = "<span>Сообщение не отправлено:" + result +"</span>";
                }
                
                loadingImg.hide();
                img_cont.hide();
                
                resultServerDiv.hide();
                resultServerDiv.fadeIn("slow").html(res);
                
                inputFrom.val(fromWho);
                inputFrom.css('color', color_letters);
                
                textFeed.val(yourFeed);
                textFeed.css('color', color_letters);
            });
        }
        return false;
    });
});